#include<func.h>
#include"head.h"
#include"threadPool.h"
#include"taskQueue.h"
#include "account.h"
#include "virtualFileSystem.h"
#include "token.h"

/* int func1_ls(int netFd,int pre_id);//ls 列出相应目录文件 
int func2(char *pathBuf, int netFd);// cd 进入对应目录
int func3(int netFd);//pwd 显示目前所在路径
int func4(int netFd);//puts 将本地文件上传至客户端
int func5(int netFd);//gets 文件名 接收客户端文件到本地
int func6(int netFd);//删除文件
int func7(int netFd);//查看日志
//int func8(void); */

void *threadFunc(void *parg){
    threadPool_t *pThreadPool = (threadPool_t *)parg;
    while(1){
        int netFd;
        char token[300] = {0};
        char tokenSalt[100] = {0};
        pthread_mutex_lock(&pThreadPool->mutex);
        pthread_cleanup_push(unlock,&pThreadPool->mutex);
        while(pThreadPool->taskQueue.size == 0 && pThreadPool->isThreadPoolRunning == 1){
            pthread_cond_wait(&pThreadPool->cond,&pThreadPool->mutex);
        }
        if(pThreadPool->isThreadPoolRunning == 0){
            puts("child quit");
            pthread_exit(NULL);
        }
        puts("GetTask!");
        netFd = pThreadPool->taskQueue.pFront->netFd;
        strcpy(token,pThreadPool->taskQueue.pFront->token);
        strcpy(tokenSalt,pThreadPool->taskQueue.pFront->tokenSalt);
        deQueue(&pThreadPool->taskQueue);
        pthread_cleanup_pop(1);

        //服务器端功能界面代码段
        handleEvent(netFd,token,tokenSalt);
        //客户端断开连接，释放该线程
        close(netFd);
        printf("thread done! tid = %lu\n",pthread_self());
    }
}


int handleEvent(int netFd,char *token,char *tokenSalt){
    char userName[50] = {0};
    char loadtime[50] = {0};
    int dataLength;
    //接收用户名
    puts("1");
    recv(netFd,&dataLength,sizeof(dataLength),0);
    recv(netFd,userName,dataLength,0);
    //接收登录时间
       puts("2");
    recv(netFd,&dataLength,sizeof(dataLength),0);
    recv(netFd,loadtime,dataLength,0);
    //拼接登录时间与用户名，来生成token码，与客户端发来的token码来对比
    //拼接格式为 loadtime + userName
    strcat(loadtime,userName);
    puts(userName);
    puts(loadtime);
    puts(crypt(loadtime,tokenSalt));
    puts("3");
    //验证token码
    int boolload;
    if(strcmp(token,crypt(loadtime,tokenSalt)) == 0){
        puts("验证成功");
        boolload = 1;
    }
    else{
        boolload = 0;
    }
    //发送验证结果
    printf("boolload = %d\n",boolload);
    send(netFd,&boolload,sizeof(boolload),0);


    //接收上传或下载指令
    int chose;
    recv(netFd,&chose,sizeof(chose),0);
    //chose 4:上传
    if(chose == 4){
        uploadSever(netFd,"shuai",3);
    }
    else if(chose == 5){
        downSever(netFd,3);
    }


}



//根据指令功选择调用
/* int handleEvent(int netFd){
    //用户登录界面
    //服务端与客户端共存一个用户名
    char *userName = (char *)calloc(50,sizeof(char));
    loadSever(netFd,&userName);
    //客户端与服务端共存一个路径地址
    int path[50] = {0};
    int count = 0;
    //获取当前用户的根目录
    path[0] = getOriginId(userName);

    while(1){
         char command_buf[1024]={0};
         int ret = recv(netFd,command_buf,sizeof(command_buf),MSG_NOSIGNAL);
         if(ret == 0){puts("客户端关闭，结束连接");break;}
         int command = atoi(command_buf);

         if(command == 0){
             break;
         }
         printf("收到指令%d\n",command);

         char pathBuf[1024]={0};
         switch (command)
         {
         case 1:{
             func1_ls(netFd,path[count]);
             break;
         }

         case 2:
             func2(pathBuf, netFd);
             break;

         case 3:
             func3(netFd);
             break;

         case 4:
             func4(netFd);
             break;

         case 5:
             func5(netFd);
             break;

         case 6:
             func6(netFd);
             break;

         case 7:
             func7(netFd);
             break;

         case 8:
             //func8();
             break;

         default:
             printf("error\n");
             break;
         }
    }
    
} */


/* int func1_ls(int netFd,int pre_id){
    //ls_c(netFd);
    lsSever(netFd,pre_id);
}

int func2(char *pathBuf, int netFd){
    recv(netFd,pathBuf,sizeof(pathBuf),0);
    puts(pathBuf);
    cd_c(pathBuf, netFd);
}

int func3(int netFd){
    pwd(netFd);
}

int func4(int netFd){
    uploadSever(netFd,"shuai",3);
    //recvFile(netFd);
}

int func5(int netFd){
    downSever(netFd,3);
    // char buf[1024]={0};
    // bzero(buf,sizeof(buf));
    // recv(netFd,buf,sizeof(buf),0);
    // sendFile(buf,netFd);//传入文件名
}

int func6(int netFd){
    remove_net(netFd);
}

int func7(int netFd){
    viewlog(netFd);
}

//int func8(int netFd); */